package cn.adviceclient.gui.click.neverlose.components;

import cn.adviceclient.features.value.*;
import cn.adviceclient.features.value.slider.NumberSetting;
import cn.adviceclient.gui.click.neverlose.NeverLose;
import cn.adviceclient.gui.click.neverlose.components.settings.*;
import cn.adviceclient.gui.font.Fonts;
import cn.adviceclient.utility.client.input.MouseUtil;
import cn.adviceclient.utility.render.ColorUtils;
import cn.adviceclient.utility.render.RenderUtils;
import cn.adviceclient.utility.render.RoundedUtils;
import cn.adviceclient.utility.render.animation.advanced.Animation;
import cn.adviceclient.utility.render.animation.advanced.Direction;
import cn.adviceclient.utility.render.animation.advanced.impl.DecelerateAnimation;
import cn.adviceclient.features.module.Module;
import cn.adviceclient.gui.click.Component;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

import java.awt.*;

import static cn.adviceclient.gui.click.neverlose.NeverLose.*;


public class ModuleComponent extends Component {
    private final Module module;
    private int scroll = 0;
    private boolean left = true;
    private final ObjectArrayList<Component> components = new ObjectArrayList<>();
    private final Animation enabled = new DecelerateAnimation(250,1);
    private final Animation hover = new DecelerateAnimation(250,1);
    public ModuleComponent(Module module) {
        this.module = module;
        for (AbstractSetting<?> setting : module.getSettings()) {
            if (setting instanceof BooleanSetting bool) {
                components.add(new BooleanComponent(bool));
            }
            if (setting instanceof NumberSetting<?> slider) {
                components.add(new SliderComponent(slider));
            }
            if (setting instanceof ModeSetting mode) {
                components.add(new ModeComponent(mode));
            }
            if (setting instanceof ColorSetting color) {
                components.add(new ColorPickerComponent(color));
            }
            if (setting instanceof StringSetting string) {
                components.add(new StringComponent(string));
            }
        }
        enabled.setDirection(module.isEnabled() ? Direction.FORWARDS : Direction.BACKWARDS);
        hover.setDirection(Direction.BACKWARDS);
    }
    @Override
    public void drawScreen(int mouseX, int mouseY) {
        float y = getY() + 6 + scroll;
        //name
        Fonts.interSemiBold.get(14).drawString(module.getName().replaceAll("(?<=[a-z])(?=[A-Z])", " ").toUpperCase(),getX() + 4,y,moduleTextRGB);
        //rect
        RoundedUtils.drawRoundOutline(getX(),y + 10,getWidth() / 2,getHeight(),4,0.1f,bgColor4,outlineColor);
        //enable
        enabled.setDirection(module.isEnabled() ? Direction.FORWARDS : Direction.BACKWARDS);
        hover.setDirection(MouseUtil.isHovered2(getX() + 154,y + 16,20,10,mouseX,mouseY) ? Direction.FORWARDS : Direction.BACKWARDS);
        Fonts.interSemiBold.get(18).drawString("Enabled",getX() + 6,y + 18,textRGB);
        RoundedUtils.drawRound(getX() + 154,y + 16,20,10,4,new Color(ColorUtils.interpolateColor2(new Color(ColorUtils.interpolateColor2(boolBgColor,boolBgColor2,(float) enabled.getOutput())),
                new Color(ColorUtils.interpolateColor2(boolBgColor,boolBgColor2,(float) enabled.getOutput())).brighter().brighter(), (float) hover.getOutput())));
        RenderUtils.drawCircle(getX() + 159 + 10 * (float) enabled.getOutput(),y + 21,0,360,5,.1f,true, ColorUtils.interpolateColor2(new Color(ColorUtils.interpolateColor2(boolCircleColor2,boolCircleColor,(float) enabled.getOutput())),
                new Color(ColorUtils.interpolateColor2(boolCircleColor.darker().darker(),boolCircleColor,(float) enabled.getOutput())).brighter().brighter(), (float) hover.getOutput()));
        float componentY = y + 22;
        ObjectArrayList<Component> filtered = components.stream()
                .filter(Component::isVisible)
                .collect(ObjectArrayList::new, ObjectArrayList::add, ObjectArrayList::addAll);
        for (Component component : filtered) {
            component.setX(getX());
            component.setY(componentY);
            component.drawScreen(mouseX, mouseY);
            componentY += component.getHeight();
        }
        super.drawScreen(mouseX, mouseY);
    }
    @Override
    public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
        if (MouseUtil.isHovered2(getX() + 154,getY() + scroll + 22,20,10,mouseX,mouseY) && mouseButton == 0){
            module.toggle();
        }
        for (Component component : components) {
            component.mouseClicked(mouseX, mouseY, mouseButton);
        }
        super.mouseClicked(mouseX, mouseY, mouseButton);
    }

    @Override
    public void mouseReleased(int mouseX, int mouseY, int state) {
        for (Component component : components) {
            component.mouseReleased(mouseX, mouseY, state);
        }
        super.mouseReleased(mouseX, mouseY, state);
    }

    @Override
    public void keyTyped(char typedChar, int keyCode) {
        for (Component component : components) {
            component.keyTyped(typedChar, keyCode);
        }
        super.keyTyped(typedChar, keyCode);
    }
    public int getMaxScroll() {
        return (int) (((getY() - NeverLose.getPosY()) + getHeight()) * 4);
    }


    public Module getModule(){
        return module;
    }


    public ObjectArrayList<Component> getComponents() {
        return components;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public boolean isLeft() {
        return left;
    }

    public int getScroll() {
        return scroll;
    }

    public void setScroll(int scroll) {
        this.scroll = scroll;
    }
}
